package org.example.back.controller.advice;

import org.example.back.service.advice.UserSleepAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/health/sleep")
public class UserSleepAdviceController {

    @Autowired
    private UserSleepAdviceService userSleepAdviceService;

    /**
     * 获取日建议（默认昨天）
     */
    @GetMapping("/advice/day")
    public String getDailyAdvice(@RequestParam(required = false) String date, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        LocalDate queryDate = (date != null) ? LocalDate.parse(date) : LocalDate.now().minusDays(1);
        return userSleepAdviceService.getDailyAdvice(userId, queryDate);
    }

    /**
     * 获取周建议（最近7天）
     */
    @GetMapping("/advice/week")
    public String getWeeklyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return userSleepAdviceService.getWeeklyAdvice(userId);
    }

    /**
     * 获取月建议（最近一个月，按周聚合）
     */
    @GetMapping("/advice/month")
    public String getMonthlyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return userSleepAdviceService.getMonthlyAdvice(userId);
    }
}
